package postpic.app;

import java.io.BufferedInputStream;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONObject;

import postpic.app.io.ClientIO;
import postpic.domain.Photo;
import postpic.domain.action.Action;
import postpic.domain.session.Request;
import postpic.domain.session.Response;
import android.app.Activity;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.ImageView;
import android.widget.Toast;

public class PhotoStreamActivity extends Activity {
	
	public List<Photo> photos;
	public List<Bitmap> bitmaps;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
	    super.onCreate(savedInstanceState);
	    setContentView(R.layout.photostream);
	    
	    Gallery gallery = (Gallery) findViewById(R.id.gallery);
	    gallery.setAdapter(new ImageAdapter(this));

	    gallery.setOnItemClickListener(new OnItemClickListener() {
	        public void onItemClick(AdapterView parent, View v, int position, long id) {
	            Toast.makeText(PhotoStreamActivity.this, "" + position, Toast.LENGTH_SHORT).show();
	        }
	    });
	    
	    JSONObject photoStream = new JSONObject();
	    
	    ClientIO.sendRequest(new Request(Action.GET_RECENT_PHOTOS, photoStream));
	    Response response = ClientIO.getResponse();
	    JSONArray photoArray = response.getResponse().getJSONArray("photos");
	    photos = new ArrayList<Photo>();
	    for (int i = 0; i < photoArray.length(); i++) {
	    	Photo photo = new Photo();
	    	photo.populateFromJSON((JSONObject) photoArray.get(i));
	    	photos.add(photo);
	    }
	    
	    bitmaps = new ArrayList<Bitmap>();
	    InputStream stream = ClientIO.getInputStream();
	    BufferedInputStream in = new BufferedInputStream(stream);
	    
	    for (int i = 0; i < photos.size(); i++) {
			Bitmap bitmap = BitmapFactory.decodeStream(in);
			bitmaps.add(bitmap);
	    }
	   
	    ImageView image = (ImageView) findViewById(R.id.imageView1);
        image.setImageBitmap(bitmaps.get(0));
	
	}
	
	public class ImageAdapter extends BaseAdapter {
	    private Context mContext;

	    public ImageAdapter(Context c) {
	        mContext = c;
	    }

	    public int getCount() {
	        return bitmaps.size();
	    }

	    public Bitmap getItem(int position) {
	        return bitmaps.get(position);
	    }

	    public long getItemId(int position) {
	        return photos.get(position).getPhotoId();
	    }

	    public View getView(int position, View convertView, ViewGroup parent) {
	        ImageView imageView = new ImageView(mContext);

	        imageView.setImageBitmap(bitmaps.get(position));
	        imageView.setLayoutParams(new Gallery.LayoutParams(150, 100));
	        imageView.setScaleType(ImageView.ScaleType.FIT_XY);

	        return imageView;
	    }
	}

}
